package com.elinkway.infinitemovies.utils;

import android.os.Bundle;
import android.text.TextUtils;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;

import java.util.Map;
import java.util.TreeMap;

/**
 * Author: liuxiaojiang(liuxiaojiang@lvideo.com)
 * Date: 2016-03-14
 * Time: 15:16
 * Description: FIXME
 */
public class SignChecker {
    public synchronized static String getSign(Bundle bundle) {
        if(bundle == null){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Map<String, String> treeMap = new TreeMap<String, String>();
        for (String key : bundle.keySet()){
            if(!TextUtils.isEmpty(bundle.getString(key))){
                treeMap.put(key, bundle.getString(key));
            }
        }
        for (Map.Entry<String, String> entry : treeMap.entrySet()){
            sb.append(entry.getKey()).append(entry.getValue());
        }
        String timeStamp = bundle.getString(MoviesHttpApi.TS);
        if (TextUtils.isEmpty(timeStamp)) {
            return "";
        }
        String token = TokenUtil.getToken(MoviesApplication.getInstance(), timeStamp);
        sb.append(token);
        String sig = MD5Utils.md5(sb.toString().replaceAll("\n", ""));
        return sig;
    }
}
